<template>
  <div class="mod-role">
    <el-card>
      <el-form
        :inline="true"
        :model="searchForm"
        @keyup.enter.native="handleSearch()"
      >
        <!-- <el-form-item>
          <el-input
            v-model="searchForm.companyName"
            placeholder="公司应用名称"
            clearable
          ></el-input>
        </el-form-item> -->
        <el-form-item>
          <!-- <el-button @click="handleSearch()">查询</el-button> -->
          <el-button
            v-if="isAuth('sys:role:save')"
            type="primary"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
          <!-- <el-button
            v-if="isAuth('sys:role:delete')"
            type="danger"
            @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0"
            >批量删除</el-button
          > -->
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 20px">
      <!-- @selection-change="selectionChangeHandle" -->
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(12,42,98, 0.9)"
        style="width: 100%"
      >
        <!-- <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        >
        </el-table-column> -->
        <el-table-column
          prop="index"
          header-align="center"
          align="center"
          width="80"
          type="index"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          prop="companyName"
          header-align="center"
          align="center"
          label="公司应用名称"
        >
        </el-table-column>
        <el-table-column
          prop="corpId"
          header-align="center"
          align="center"
          label="企业ID"
        >
        </el-table-column>
        <el-table-column
          prop="agentId"
          header-align="center"
          align="center"
          label="企业应用ID"
        >
        </el-table-column>
        <el-table-column
          prop="sendSecret"
          header-align="center"
          align="center"
          label="消息推送应用密钥"
        >
        </el-table-column>
        <el-table-column
          prop="bookSecret"
          header-align="center"
          align="center"
          label="通讯录密钥"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          header-align="center"
          align="center"
          label="备注"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          width="180"
          label="创建时间"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              v-if="isAuth('sys:role:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row)"
              >修改</el-button
            >
            <el-button
              v-if="isAuth('sys:role:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
    <!-- 弹窗, 新增 / 修改 -->
    <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="addOrUpdateVisible"
      width="35%"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="150px"
      >
        <el-form-item label="公司应用名称" prop="companyName">
          <el-input
            v-model="dataForm.companyName"
            placeholder="请输入公司应用名称"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业ID" prop="corpId">
          <el-input
            v-model="dataForm.corpId"
            placeholder="请输入ID"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业应用ID" prop="agentId">
          <el-input
            v-model="dataForm.agentId"
            placeholder="请输入应用ID"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
        <el-form-item label="消息推送应用密钥" prop="sendSecret">
          <el-input
            v-model="dataForm.sendSecret"
            placeholder="请输入消息推送应用密钥"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
        <el-form-item label="通讯录密钥" prop="bookSecret">
          <el-input
            v-model="dataForm.bookSecret"
            placeholder="请输入通讯录密钥"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="">
          <el-input
            v-model="dataForm.remark"
            placeholder="请输入备注"
            style="width: 21.875rem"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addOrUpdateVisible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit('dataForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        companyName: "",
      },
      dataForm: {
        id: null,
        companyName: "", //公司应用名称
        corpId: "", //企业ID
        agentId: "", //企业应用ID
        sendSecret: "", //消息推送应用密钥
        bookSecret: "", //通讯录密钥
        remark: "", //备注
        createTime: "", //创建时间
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dataRule: {
        companyName: [
          { required: true, message: "请输入公司应用名称", trigger: "blur" },
        ],
        corpId: [{ required: true, message: "请输入企业ID", trigger: "blur" }],
        agentId: [
          { required: true, message: "请输入企业应用ID", trigger: "blur" },
        ],
        sendSecret: [
          {
            required: true,
            message: "请输入消息推送应用密钥",
            trigger: "blur",
          },
        ],
        bookSecret: [
          { required: true, message: "请输入通讯录密钥", trigger: "blur" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
    };
  },
  components: {},
  activated() {
    this.getdaysTow();
    this.getDataList();
  },
  watch: {
    addOrUpdateVisible(newVal, oldVal) {
      if (newVal === false) {
        this.$refs["dataForm"].resetFields();
        // this.$refs.dataForm.clearValidate();
      }
    },
  },
  methods: {
    getdaysTow() {
      this.dataForm.createTime = "";
      var date = new Date();
      //年 getFullYear()：四位数字返回年份
      var y = date.getFullYear(); //getFullYear()代替getYear()
      //月 getMonth()：0 ~ 11
      var m = date.getMonth() + 1;
      //日 getDate()：(1 ~ 31)
      var d = date.getDate();
      //时 getHours()：(0 ~ 23)
      var h = date.getHours();
      //分 getMinutes()： (0 ~ 59)
      var mm = date.getMinutes();
      //秒 getSeconds()：(0 ~ 59)
      var s = date.getSeconds();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      mm = mm < 10 ? "0" + mm : mm;
      s = s < 10 ? "0" + s : s;
      console.log(
        "============",
        y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s
      );
      this.dataForm.createTime =
        y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/wecomConfig/searchList"),
        method: "get",
        params: this.$http.adornParams({
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          companyName: this.searchForm.companyName,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          setTimeout(() => {
            this.dataListLoading = false;
            this.dataList = data.data.records;
            this.totalPage = data.data.total;
          }, 1000);
        } else {
          this.$message.error(res.data.msg);
          this.dataList = [];
          this.totalPage = 0;
        }
        // this.dataListLoading = false
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    //查询
    handleSearch() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.addOrUpdateVisible = true;
      console.log("row==", row);
      if (row) {
        this.dataForm = JSON.parse(JSON.stringify(row));
        console.log("编辑");
      } else {
        console.log("新增");
        this.dataForm.companyName = "";
        this.dataForm.corpId = "";
        this.dataForm.agentId = "";
        this.dataForm.sendSecret = "";
        this.dataForm.bookSecret = "";
        this.dataForm.remark = "";
        this.dataForm.id = null;
      }
      this.getdaysTow();
    },
    //确认
    dataFormSubmit(dataForm) {
      this.$refs[dataForm].validate((valid) => {
        if (valid) {
          console.log("this.dataForm===", this.dataForm);
          console.log("this.dataForm===", this.dataForm.companyName);
          console.log("this.dataForm===", this.dataForm.createTime);
          this.$http({
            url: this.$http.adornUrl(
              `/wecomConfig/${!this.dataForm.id ? "insert" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              companyName: this.dataForm.companyName,
              corpId: this.dataForm.corpId,
              agentId: this.dataForm.agentId,
              sendSecret: this.dataForm.sendSecret,
              bookSecret: this.dataForm.bookSecret,
              remark: this.dataForm.remark,
              createTime: this.dataForm.createTime,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.addOrUpdateVisible = false;
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
        // else {
        //   console.log("error submit!!");
        //   return false;
        // }
      });
    },
    // 删除
    deleteHandle(id) {
      //   var ids = id
      //     ? [id]
      //     : this.dataListSelections.map((item) => {
      //         return item.roleId;
      //       });
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/wecomConfig/delete"),
            method: "get",
            params: this.$http.adornParams({ id }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
